package com.sfh.cp.mass;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sfh.cp.R;
import com.sfh.cp.base.AbstractBaseActivity;
import com.sfh.cp.mass.pst.IPstMassAddMessage;
import com.sfh.cp.mass.req.ReqAddMessage;
import com.sfh.cp.mass.req.RxNoteMessageSize;
import com.sfh.cp.util.EmotionKeyboard;
import com.sfh.cp.util.UtilFace;
import com.sfh.cp.widget.WidgetFaceLayout;
import com.sfh.lib.RxBusEventManager;
import com.sfh.lib.mvp.IPresenter;
import com.sfh.lib.mvp.service.LoaderService;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import crm.sfh.server.entity.EMass;
import crm.sfh.server.entity.ENote;

/**
 * 功能描述:
 *
 * @author
 * @date 2018/5/2
 */
public class MassAddMessageActivity extends AbstractBaseActivity implements IPstMassAddMessage.IVAddMessage {

    public static void startUI(Context context, ENote note) {
        Intent intent = new Intent(context, MassAddMessageActivity.class);
        intent.putExtra("DATA", note.getNoteId());
        context.startActivity(intent);
    }

    @BindView(R.id.etContent)
    EditText etContent;

    @BindView(R.id.ivFace)
    ImageView ivFace;

    @BindView(R.id.lyContent)
    LinearLayout lyContent;

    @BindView(R.id.face_layout)
    WidgetFaceLayout faceLayout;

    String noteId;
    EmotionKeyboard mEmotionKeyboard;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.mass_add_message_activity);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        noteId = getIntent().getStringExtra("DATA");
        mEmotionKeyboard = EmotionKeyboard.with(this)
                .setEmotionLayout(faceLayout)
                .bindToContent(lyContent)
                .bindToEditText(etContent)
                .bindToEmotionButton(ivFace)
                .build();
        faceLayout.addOnItemSelectListener(new WidgetFaceLayout.OnItemSelectListener() {
            @Override
            public void onClickSelect(String name, int res) {
                String content = etContent.getText().toString().trim() + name;
                SpannableString spannableString = UtilFace.getEmotionContent(MassAddMessageActivity.this, etContent, content);
                etContent.setText(spannableString);
            }
        });
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                etContent.setSelection(s.length());
            }
        });
    }

    IPstMassAddMessage pstMassAddMessage;

    @Override
    public IPresenter getPresenter() {

        if (pstMassAddMessage == null) {
            pstMassAddMessage = LoaderService.getService(IPstMassAddMessage.class);
        }
        return pstMassAddMessage;
    }

    @OnClick(R.id.btAdd)
    public void onClick() {

        ReqAddMessage req = new ReqAddMessage();
        req.setContent(etContent.getText().toString());
        req.setNoteId(noteId);
        pstMassAddMessage.addMessage(req);

    }

    @Override
    public void onSuccess() {
        finish();
    }
}
